/**
 * 核心技术支持
 * IT渠道网 小程序商城
 * 13873174181 微信同号
 */
import {My} from '../../utils/my.js';
var my = new My();

Page({
    data: {
        pageIndex: 1,
        loadingHidden: false,
        isLoadedAll: false,
        goodsArr: [],
        totalPage: 0
    },
    onLoad: function() {
        this._getCollection()
    },
    /**
     * 获取收藏数据
     */
    _getCollection: function() {
        var that = this,
            page = this.data.pageIndex;
        my.getCollection(page, (data)=>{
            data.list.forEach((item) => {
                item.price = item.price.toFixed(2);
            })
            that.data.goodsArr.push.apply(that.data.goodsArr, data.list);
            that.setData({
                goodsArr: that.data.goodsArr,
                totalPage: data.totalPage,
                loadingHidden: true
            })
        })
    },
    /**
     * 监听用户下拉动作 重新刷新用户收藏,回到&page=1
     */
    onPullDownRefresh: function() {
        var that = this;
        that.data.goodsArr = [];  // 初始化
        that.data.pageIndex = 1;
        that._getCollection(() => {
            that.setData({
                isLoadedAll: false
            });
            wx.stopPullDownRefresh()
        })
    },
    /**
     * 页面上拉触底事件的处理函数 上拉到底部触发&page=totalPage+1
     */
    onReachBottom: function() {
        if (!this.data.isLoadedAll && this.data.pageIndex < this.data.totalPage) {
            this.data.pageIndex++;
            this._getCollection();
        }
        if (this.data.pageIndex == this.data.totalPage){
            this.setData({
                isLoadedAll: true
            })
        }
    }
})